home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj0492.zip / DFLT11.ZIP / VIDEO.C < prev    next >
Text File  |  1992-02-29  |  7KB  |  269 lines

  1. /* --------------------- video.c -------------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. BOOL ClipString;
  6.  
  7. static unsigned video_address;
  8.  
  9. /* -- swap a rectangle of video memory with a save buffer -- */
  10. void swapvideo(WINDOW wnd, void far *bf)
  11. {
  12.     char *hd;
  13.     int ht, bytes_row, bufferwidth;
  14.     RECT rc = WindowRect(wnd);
  15.     unsigned vadr = vad(RectLeft(rc), RectTop(rc));
  16.  
  17.     if (TestAttribute(wnd, SHADOW))    {
  18.         RectBottom(rc)++;
  19.         RectRight(rc)++;
  20.     }
  21.     ht = RectBottom(rc)-RectTop(rc)+1;
  22.  
  23.     if (RectTop(rc) + ht > SCREENHEIGHT)
  24.         ht = SCREENHEIGHT - RectTop(rc);
  25.  
  26.     bufferwidth = bytes_row = (RectRight(rc)-RectLeft(rc)+1) * 2;
  27.  
  28.     if (RectLeft(rc) + bytes_row/2 > SCREENWIDTH)
  29.         bytes_row = (SCREENWIDTH - RectLeft(rc)) * 2;
  30.  
  31.     hd = malloc(bytes_row);
  32.  
  33.     hide_mousecursor();
  34.     while (ht--)    {
  35.         movedata(video_address, vadr, FP_SEG(hd),
  36.                 FP_OFF(hd), bytes_row);
  37.         movedata(FP_SEG(bf), FP_OFF(bf), video_address,
  38.                 vadr, bytes_row);
  39.         movedata(FP_SEG(hd), FP_OFF(hd), FP_SEG(bf),
  40.                 FP_OFF(bf), bytes_row);
  41.         vadr += SCREENWIDTH*2;
  42.         bf = (char far *)bf + bufferwidth;
  43.     }
  44.     free(hd);
  45.     show_mousecursor();
  46. }
  47.  
  48. /* -- read a rectangle of video memory into a save buffer -- */
  49. void getvideo(RECT rc, void far *bf)
  50. {
  51.     int ht = RectBottom(rc)-RectTop(rc)+1;
  52.     int bytes_row = (RectRight(rc)-RectLeft(rc)+1) * 2;
  53.     unsigned vadr = vad(RectLeft(rc), RectTop(rc));
  54.     hide_mousecursor();
  55.     while (ht--)    {
  56.         movedata(video_address, vadr, FP_SEG(bf),
  57.                 FP_OFF(bf), bytes_row);
  58.         vadr += SCREENWIDTH*2;
  59.         bf = (char far *)bf + bytes_row;
  60.     }
  61.     show_mousecursor();
  62. }
  63.  
  64. /* -- write a rectangle of video memory from a save buffer -- */
  65. void storevideo(RECT rc, void far *bf)
  66. {
  67.     int ht = RectBottom(rc)-RectTop(rc)+1;
  68.     int bytes_row = (RectRight(rc)-RectLeft(rc)+1) * 2;
  69.     unsigned vadr = vad(RectLeft(rc), RectTop(rc));
  70.     hide_mousecursor();
  71.     while (ht--)    {
  72.         movedata(FP_SEG(bf), FP_OFF(bf), video_address,
  73.                 vadr, bytes_row);
  74.         vadr += SCREENWIDTH*2;
  75.         bf = (char far *)bf + bytes_row;
  76.     }
  77.     show_mousecursor();
  78. }
  79.  
  80. /* -------- read a character of video memory ------- */
  81. unsigned int GetVideoChar(int x, int y)
  82. {
  83.     int c;
  84.     hide_mousecursor();
  85.     c = peek(video_address, vad(x,y));
  86.     show_mousecursor();
  87.     return c;
  88. }
  89.  
  90. /* -------- write a character of video memory ------- */
  91. void PutVideoChar(int x, int y, int c)
  92. {
  93.     if (x < SCREENWIDTH && y < SCREENHEIGHT)    {
  94.         hide_mousecursor();
  95.         poke(video_address, vad(x,y), c);
  96.         show_mousecursor();
  97.     }
  98. }
  99.  
  100. static BOOL isAncestor(WINDOW wnd, WINDOW awnd)
  101. {
  102.     while (wnd != NULL)    {
  103.         if (wnd == awnd)
  104.             return TRUE;
  105.         wnd = GetParent(wnd);
  106.     }
  107.     return FALSE;
  108. }
  109.  
  110. BOOL CharInView(WINDOW wnd, int x, int y)
  111. {
  112.     WINDOW nwnd = NextWindow(wnd);
  113.     WINDOW pwnd;
  114.     RECT rc;
  115.     int x1 = GetLeft(wnd)+x;
  116.     int y1 = GetTop(wnd)+y;
  117.  
  118.     if (!TestAttribute(wnd, VISIBLE))
  119.         return FALSE;
  120.     if (!TestAttribute(wnd, NOCLIP))    {
  121.         WINDOW wnd1 = GetParent(wnd);
  122.         while (wnd1 != NULL)    {
  123.             /* --- clip character to parent's borders -- */
  124.             if (!TestAttribute(wnd1, VISIBLE))
  125.                 return FALSE;
  126.             if (!InsideRect(x1, y1, ClientRect(wnd1)))
  127.                 return FALSE;
  128.             wnd1 = GetParent(wnd1);
  129.         }
  130.     }
  131.     while (nwnd != NULL)    {
  132.         if (!isHidden(nwnd) && !isAncestor(wnd, nwnd))    {
  133.             rc = WindowRect(nwnd);
  134.             if (TestAttribute(nwnd, SHADOW))    {
  135.                 RectBottom(rc)++;
  136.                 RectRight(rc)++;
  137.             }
  138.             if (!TestAttribute(nwnd, NOCLIP))    {
  139.                 pwnd = nwnd;
  140.                 while (GetParent(pwnd))    {
  141.                     pwnd = GetParent(pwnd);
  142.                     rc = subRectangle(rc, ClientRect(pwnd));
  143.                 }
  144.             }
  145.             if (InsideRect(x1,y1,rc))
  146.                 return FALSE;
  147.         }
  148.         nwnd = NextWindow(nwnd);
  149.     }
  150.     return (x1 < SCREENWIDTH && y1 < SCREENHEIGHT);
  151. }
  152.  
  153. /* -------- write a character to a window ------- */
  154. void wputch(WINDOW wnd, int c, int x, int y)
  155. {
  156.     if (CharInView(wnd, x, y))    {
  157.         hide_mousecursor();
  158.         poke(video_address,
  159.             vad(GetLeft(wnd)+x,GetTop(wnd)+y),(c & 255) |
  160.                 (clr(foreground, background) << 8));
  161.         show_mousecursor();
  162.     }
  163. }
  164.  
  165. /* ------- write a string to a window ---------- */
  166. void wputs(WINDOW wnd, void *s, int x, int y)
  167. {
  168.     int x1 = GetLeft(wnd)+x;
  169.     int x2 = x1;
  170.     int y1 = GetTop(wnd)+y;
  171.     if (x1 < SCREENWIDTH && y1 < SCREENHEIGHT && isVisible(wnd))    {
  172.         int ln[200];
  173.         int *cp1 = ln;
  174.         unsigned char *str = s;
  175.         int fg = foreground;
  176.         int bg = background;
  177.         int len;
  178.         int off = 0;
  179.         while (*str)    {
  180.             if (*str == CHANGECOLOR)    {
  181.                 str++;
  182.                 foreground = (*str++) & 0x7f;
  183.                 background = (*str++) & 0x7f;
  184.                 continue;
  185.             }
  186.             if (*str == RESETCOLOR)    {
  187.                 foreground = fg & 0x7f;
  188.                 background = bg & 0x7f;
  189.                 str++;
  190.                 continue;
  191.             }
  192.                *cp1 = (*str & 255) | (clr(foreground, background) << 8);
  193.             if (ClipString)
  194.                 if (!CharInView(wnd, x, y))
  195.                     *cp1 = peek(video_address, vad(x2,y1));
  196.             cp1++;
  197.             str++;
  198.             x++;
  199.             x2++;
  200.         }
  201.         foreground = fg;
  202.         background = bg;
  203.            len = (int)(cp1-ln);
  204.            if (x1+len > SCREENWIDTH)
  205.                len = SCREENWIDTH-x1;
  206.  
  207.         if (!ClipString && !TestAttribute(wnd, NOCLIP))    {
  208.             /* -- clip the line to within ancestor windows -- */
  209.             RECT rc = WindowRect(wnd);
  210.             WINDOW nwnd = GetParent(wnd);
  211.             while (len > 0 && nwnd != NULL)    {
  212.                 if (!isVisible(nwnd))    {
  213.                     len = 0;
  214.                     break;
  215.                 }
  216.                 rc = subRectangle(rc, ClientRect(nwnd));
  217.                 nwnd = GetParent(nwnd);
  218.             }
  219.             while (len > 0 && !InsideRect(x1+off,y1,rc))    {
  220.                 off++;
  221.                 --len;
  222.             }
  223.             if (len > 0)    {
  224.                 x2 = x1+len-1;
  225.                 while (len && !InsideRect(x2,y1,rc))    {
  226.                     --x2;
  227.                     --len;
  228.                 }
  229.             }
  230.         }
  231.         if (len > 0)    {
  232.             hide_mousecursor();
  233.             movedata(FP_SEG(ln), FP_OFF(ln+off),
  234.                 video_address, vad(x1+off,y1), len*2);
  235.             show_mousecursor();
  236.         }
  237.     }
  238. }
  239.  
  240. /* --------- get the current video mode -------- */
  241. void get_videomode(void)
  242. {
  243.     videomode();
  244.     /* ---- Monochrome Display Adaptor or text mode ---- */
  245.     if (ismono())
  246.         video_address = 0xb000;
  247.     else
  248.         /* ------ Text mode -------- */
  249.         video_address = 0xb800 + video_page;
  250. }
  251.  
  252. /* --------- scroll the window. d: 1 = up, 0 = dn ---------- */
  253. void scroll_window(WINDOW wnd, RECT rc, int d)
  254. {
  255.     union REGS regs;
  256.     hide_mousecursor();
  257.     regs.h.cl = RectLeft(rc);
  258.     regs.h.ch = RectTop(rc);
  259.     regs.h.dl = RectRight(rc);
  260.     regs.h.dh = RectBottom(rc);
  261.     regs.h.bh = clr(WndForeground(wnd),WndBackground(wnd));
  262.     regs.h.ah = 7 - d;
  263.     regs.h.al = 1;
  264.     int86(VIDEO, ®s, ®s);
  265.     show_mousecursor();
  266. }
  267.  
  268.  
  269.